package top.kuxiao.crmserver.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.*;
import top.kuxiao.crmserver.service.IAccountManagerService;
import top.kuxiao.crmserver.service.ICustomerLossService;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.service.IOrdersService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户流失管理 前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-18
 */
@Api(tags = "客户流失管理")
@CrossOrigin
@RestController
@RequestMapping("/customer-loss")
public class CustomerLossController {


    @Autowired
    ICustomerLossService customerLossService;

    @Autowired
    CookieUtil cookieUtil;

    @Autowired
    ICustomerService customerService;

    @Autowired
    IAccountManagerService accountManagerService;

    /**
     * 查询该账号的流失客户
     */
    @Auth("AM")
    @ApiOperation(value = "查询该账号的流失客户")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ResultUtil list(HttpServletRequest request) {

        AccountManager accountManager = (AccountManager) cookieUtil.getObject(request);

        List<CustomerLoss> customerLosses = customerLossService.list();
        List<CustomerLoss> list = null;

        if (customerLosses != null) {

            list = new ArrayList<>();
            for (CustomerLoss customerLoss : customerLosses) {
                if (accountManager.getId().equals(customerLoss.getAccountManagerId())) {
                    customerLoss.setAccountManager(accountManager);
                    customerLoss.setCustomer(customerService.getById(customerLoss.getCustomerId()));
                    list.add(customerLoss);
                }
            }
        }

        return ResultUtil.resObj(list);
    }

    /**
     * 实现暂缓措施,
     */
    //TODO 修改
    @Auth("AM")
    @ApiImplicitParam(name = "id", value = "暂缓的id")
    @ApiOperation(value = "暂缓措施按钮", notes = "需要填写客户流失原因,暂时是拼接")
    @RequestMapping(value = "update/{id}", method = RequestMethod.PUT)
    public ResultUtil update(@PathVariable("id") Integer id, String note) {
        CustomerLoss customerLoss = customerLossService.getById(id);

        if (!("预警".equals(customerLoss.getCustomerLossState()) || "暂缓".equals(customerLoss.getCustomerLossState()))){
            return ResultUtil.resMsgError("失败");
        }

        customerLoss.setCustomerLossState("暂缓");
        String notes;
        if (customerLoss.getSuspensionOfLossMeasures() != null) {
            notes = customerLoss.getSuspensionOfLossMeasures() + "\n" + note;
        }else {
            notes=note;
        }

        customerLoss.setSuspensionOfLossMeasures(notes);



        if (customerLossService.updateById(customerLoss)) {
            return ResultUtil.resMsgOK("成功");
        }
        return ResultUtil.resMsgError("失败");
    }

    /**
     * 客户流失
     */
    @Auth("AM")
    @ApiImplicitParam(name = "id", value = "流失的id")
    @ApiOperation(value = "客户流失按钮", notes = "需要填写客户流失原因,流失了用户就不能做其他操作了")
    @RequestMapping(value = "loss/{id}", method = RequestMethod.PUT)
    public ResultUtil updateLoss(@PathVariable("id") Integer id, String note) {
        CustomerLoss customerLoss = customerLossService.getById(id);
        Customer customer = customerService.getById(customerLoss.getCustomerId());

        customerLoss.setConfirmLossReason(note);
        customerLoss.setConfirmLossTime(new Date());
        customerLoss.setCustomerLossState("流失");

        customer.setCustomerState("流失");

        if (customerService.updateById(customer) && customerLossService.updateById(customerLoss)) {
            return ResultUtil.resMsgOK("成功");
        }
        return ResultUtil.resMsgError("失败");
    }
}
